package com.lyfy.sd.order.controller;

import com.lyfy.sd.order.dao.entity.OrderMaster;
import com.lyfy.sd.order.dao.mapper.OrderMasterMapper;
import com.lyfy.sd.order.vo.RespVo;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import javax.annotation.Resource;

import static com.lyfy.sd.order.constant.Constant.DEFAULT_CREATOR;
import static com.lyfy.sd.order.constant.Constant.DEFAULT_USER_ID;

/**
 * @author: 谢星星
 * @date: 2022/10/3 03:11
 * @description:
 */
@RestController
public class SdDeleteController {

    @Resource
    private OrderMasterMapper orderMasterMapper;


    /**
     * 场景：分片键删除
     */
    @GetMapping(value = "/sd/del-userid", produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVo del1() {
        int cnt = orderMasterMapper.deleteByExample(Example.builder(OrderMaster.class)
                .where(WeekendSqls.<OrderMaster>custom().andEqualTo(OrderMaster::getUserId, DEFAULT_USER_ID))
                .build());
        return RespVo.SUCCESS(cnt);
    }

    /**
     * 场景：非分片键删除
     */
    @Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
    @GetMapping(value = "/sd/del-omid", produces = MediaType.APPLICATION_JSON_VALUE)
    public RespVo del2() {
        int cnt = orderMasterMapper.deleteByExample(Example.builder(OrderMaster.class)
                .where(WeekendSqls.<OrderMaster>custom().andEqualTo(OrderMaster::getCreator, DEFAULT_CREATOR))
                .build());
        return RespVo.SUCCESS(cnt);
    }

}
